package com.chengxusheji.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.UUID;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.chengxusheji.utils.ExportExcelUtil;
import com.chengxusheji.dao.CardGetRecordDAO;
import com.chengxusheji.domain.CardGetRecord;
import com.chengxusheji.dao.MemberInfoDAO;
import com.chengxusheji.domain.MemberInfo;
import com.chengxusheji.dao.MemberCardDAO;
import com.chengxusheji.domain.MemberCard;
@Controller @Scope("prototype")
public class CardGetRecordAction extends ActionSupport {

    /*界面层需要查询的属性: 领取会员*/
    private MemberInfo memberObj;
    public void setMemberObj(MemberInfo memberObj) {
        this.memberObj = memberObj;
    }
    public MemberInfo getMemberObj() {
        return this.memberObj;
    }

    /*界面层需要查询的属性: 会员卡*/
    private MemberCard cardObj;
    public void setCardObj(MemberCard cardObj) {
        this.cardObj = cardObj;
    }
    public MemberCard getCardObj() {
        return this.cardObj;
    }

    /*当前第几页*/
    private int currentPage;
    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }
    public int getCurrentPage() {
        return currentPage;
    }

    /*一共多少页*/
    private int totalPage;
    public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }
    public int getTotalPage() {
        return totalPage;
    }

    private int id;
    public void setId(int id) {
        this.id = id;
    }
    public int getId() {
        return id;
    }

    /*当前查询的总记录数目*/
    private int recordNumber;
    public void setRecordNumber(int recordNumber) {
        this.recordNumber = recordNumber;
    }
    public int getRecordNumber() {
        return recordNumber;
    }

    /*业务层对象*/
    @Resource CardGetRecordDAO cardGetRecordDAO;

    @Resource MemberInfoDAO memberInfoDAO;
    @Resource MemberCardDAO memberCardDAO;
    /*待操作的CardGetRecord对象*/
    private CardGetRecord cardGetRecord;
    public void setCardGetRecord(CardGetRecord cardGetRecord) {
        this.cardGetRecord = cardGetRecord;
    }
    public CardGetRecord getCardGetRecord() {
        return this.cardGetRecord;
    }

    /*跳转到添加CardGetRecord视图*/
    public String AddView() {
        ActionContext ctx = ActionContext.getContext();
        /*查询所有的MemberInfo信息*/
        List<MemberInfo> memberInfoList = memberInfoDAO.QueryAllMemberInfoInfo();
        ctx.put("memberInfoList", memberInfoList);
        /*查询所有的MemberCard信息*/
        List<MemberCard> memberCardList = memberCardDAO.QueryAllMemberCardInfo();
        ctx.put("memberCardList", memberCardList);
        return "add_view";
    }

    /*添加CardGetRecord信息*/
    @SuppressWarnings("deprecation")
    public String AddCardGetRecord() {
        ActionContext ctx = ActionContext.getContext();
        try {
            
            MemberInfo memberObj = memberInfoDAO.GetMemberInfoByHyzh(cardGetRecord.getMemberObj().getHyzh());
            cardGetRecord.setMemberObj(memberObj);
            
            MemberCard cardObj = memberCardDAO.GetMemberCardByCardNumber(cardGetRecord.getCardObj().getCardNumber());
            cardGetRecord.setCardObj(cardObj);
            
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            cardGetRecord.setGetTime(sdf.format(new java.util.Date()));
            
            //判断这个会员卡是否被领取
            if(cardGetRecordDAO.QueryCardGetRecordInfo(null, cardObj).size()>0) {

            	ctx.put("error",  java.net.URLEncoder.encode("该会员卡已经被领取！"));
            	return "error";
            }
            
            
            cardGetRecordDAO.AddCardGetRecord(cardGetRecord);
            ctx.put("message",  java.net.URLEncoder.encode("办理会员卡领取成功!"));
            return "add_success";
        } catch (Exception e) {
            e.printStackTrace();
            ctx.put("error",  java.net.URLEncoder.encode("CardGetRecord添加失败!"));
            return "error";
        }
    }

    /*查询CardGetRecord信息*/
    public String QueryCardGetRecord() {
        if(currentPage == 0) currentPage = 1;
        List<CardGetRecord> cardGetRecordList = cardGetRecordDAO.QueryCardGetRecordInfo(memberObj, cardObj, currentPage);
        /*计算总的页数和总的记录数*/
        cardGetRecordDAO.CalculateTotalPageAndRecordNumber(memberObj, cardObj);
        /*获取到总的页码数目*/
        totalPage = cardGetRecordDAO.getTotalPage();
        /*当前查询条件下总记录数*/
        recordNumber = cardGetRecordDAO.getRecordNumber();
        ActionContext ctx = ActionContext.getContext();
        ctx.put("cardGetRecordList",  cardGetRecordList);
        ctx.put("totalPage", totalPage);
        ctx.put("recordNumber", recordNumber);
        ctx.put("currentPage", currentPage);
        ctx.put("memberObj", memberObj);
        List<MemberInfo> memberInfoList = memberInfoDAO.QueryAllMemberInfoInfo();
        ctx.put("memberInfoList", memberInfoList);
        ctx.put("cardObj", cardObj);
        List<MemberCard> memberCardList = memberCardDAO.QueryAllMemberCardInfo();
        ctx.put("memberCardList", memberCardList);
        return "query_view";
    }

    /*后台导出到excel*/
    public String QueryCardGetRecordOutputToExcel() { 
        List<CardGetRecord> cardGetRecordList = cardGetRecordDAO.QueryCardGetRecordInfo(memberObj,cardObj);
        ExportExcelUtil ex = new ExportExcelUtil();
        String title = "CardGetRecord信息记录"; 
        String[] headers = { "领取会员","会员卡","领取时间","附加信息"};
        List<String[]> dataset = new ArrayList<String[]>(); 
        for(int i=0;i<cardGetRecordList.size();i++) {
        	CardGetRecord cardGetRecord = cardGetRecordList.get(i); 
        	dataset.add(new String[]{cardGetRecord.getMemberObj().getHyzh(),
cardGetRecord.getCardObj().getCardNumber(),
cardGetRecord.getGetTime(),cardGetRecord.getMemo()});
        }
        /*
        OutputStream out = null;
		try {
			out = new FileOutputStream("C://output.xls");
			ex.exportExcel(title,headers, dataset, out);
		    out.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		*/
		HttpServletResponse response = null;//创建一个HttpServletResponse对象 
		OutputStream out = null;//创建一个输出流对象 
		try { 
			response = ServletActionContext.getResponse();//初始化HttpServletResponse对象 
			out = response.getOutputStream();//
			response.setHeader("Content-disposition","attachment; filename="+"CardGetRecord.xls");//filename是下载的xls的名，建议最好用英文 
			response.setContentType("application/msexcel;charset=UTF-8");//设置类型 
			response.setHeader("Pragma","No-cache");//设置头 
			response.setHeader("Cache-Control","no-cache");//设置头 
			response.setDateHeader("Expires", 0);//设置日期头  
			String rootPath = ServletActionContext.getServletContext().getRealPath("/");
			ex.exportExcel(rootPath,title,headers, dataset, out);
			out.flush();
		} catch (IOException e) { 
			e.printStackTrace(); 
		}finally{
			try{
				if(out!=null){ 
					out.close(); 
				}
			}catch(IOException e){ 
				e.printStackTrace(); 
			} 
		}
		return null;
    }
    /*前台查询CardGetRecord信息*/
    public String FrontQueryCardGetRecord() {
        if(currentPage == 0) currentPage = 1;
        List<CardGetRecord> cardGetRecordList = cardGetRecordDAO.QueryCardGetRecordInfo(memberObj, cardObj, currentPage);
        /*计算总的页数和总的记录数*/
        cardGetRecordDAO.CalculateTotalPageAndRecordNumber(memberObj, cardObj);
        /*获取到总的页码数目*/
        totalPage = cardGetRecordDAO.getTotalPage();
        /*当前查询条件下总记录数*/
        recordNumber = cardGetRecordDAO.getRecordNumber();
        ActionContext ctx = ActionContext.getContext();
        ctx.put("cardGetRecordList",  cardGetRecordList);
        ctx.put("totalPage", totalPage);
        ctx.put("recordNumber", recordNumber);
        ctx.put("currentPage", currentPage);
        ctx.put("memberObj", memberObj);
        List<MemberInfo> memberInfoList = memberInfoDAO.QueryAllMemberInfoInfo();
        ctx.put("memberInfoList", memberInfoList);
        ctx.put("cardObj", cardObj);
        List<MemberCard> memberCardList = memberCardDAO.QueryAllMemberCardInfo();
        ctx.put("memberCardList", memberCardList);
        return "front_query_view";
    }

    /*查询要修改的CardGetRecord信息*/
    public String ModifyCardGetRecordQuery() {
        ActionContext ctx = ActionContext.getContext();
        /*根据主键id获取CardGetRecord对象*/
        CardGetRecord cardGetRecord = cardGetRecordDAO.GetCardGetRecordById(id);

        List<MemberInfo> memberInfoList = memberInfoDAO.QueryAllMemberInfoInfo();
        ctx.put("memberInfoList", memberInfoList);
        List<MemberCard> memberCardList = memberCardDAO.QueryAllMemberCardInfo();
        ctx.put("memberCardList", memberCardList);
        ctx.put("cardGetRecord",  cardGetRecord);
        return "modify_view";
    }

    /*查询要修改的CardGetRecord信息*/
    public String FrontShowCardGetRecordQuery() {
        ActionContext ctx = ActionContext.getContext();
        /*根据主键id获取CardGetRecord对象*/
        CardGetRecord cardGetRecord = cardGetRecordDAO.GetCardGetRecordById(id);

        List<MemberInfo> memberInfoList = memberInfoDAO.QueryAllMemberInfoInfo();
        ctx.put("memberInfoList", memberInfoList);
        List<MemberCard> memberCardList = memberCardDAO.QueryAllMemberCardInfo();
        ctx.put("memberCardList", memberCardList);
        ctx.put("cardGetRecord",  cardGetRecord);
        return "front_show_view";
    }

    /*更新修改CardGetRecord信息*/
    public String ModifyCardGetRecord() {
        ActionContext ctx = ActionContext.getContext();
        try {
            if(true) {
            MemberInfo memberObj = memberInfoDAO.GetMemberInfoByHyzh(cardGetRecord.getMemberObj().getHyzh());
            cardGetRecord.setMemberObj(memberObj);
            }
            if(true) {
            MemberCard cardObj = memberCardDAO.GetMemberCardByCardNumber(cardGetRecord.getCardObj().getCardNumber());
            cardGetRecord.setCardObj(cardObj);
            }
            cardGetRecordDAO.UpdateCardGetRecord(cardGetRecord);
            ctx.put("message",  java.net.URLEncoder.encode("CardGetRecord信息更新成功!"));
            return "modify_success";
        } catch (Exception e) {
            e.printStackTrace();
            ctx.put("error",  java.net.URLEncoder.encode("CardGetRecord信息更新失败!"));
            return "error";
       }
   }

    /*删除CardGetRecord信息*/
    public String DeleteCardGetRecord() {
        ActionContext ctx = ActionContext.getContext();
        try { 
            cardGetRecordDAO.DeleteCardGetRecord(id);
            ctx.put("message",  java.net.URLEncoder.encode("CardGetRecord删除成功!"));
            return "delete_success";
        } catch (Exception e) { 
            e.printStackTrace();
            ctx.put("error",  java.net.URLEncoder.encode("CardGetRecord删除失败!"));
            return "error";
        }
    }

}
